package com.china08.yxyapi.api.comm.upload;

import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.util.YXCSPUtil;

/**
 * 视频会议上传附件
 *
 */
@CrossOrigin
@RestController
public class UploadFileForMeetingController {

    @Autowired
    private YXCSPUtil yxcspUtil;

    @RequestMapping(value = "/api_yw/comm/uploadFile/meeting", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel upLoadWenDoc(@RequestParam(value = "file", required = false) MultipartFile file)
            throws IOException {

        RespModel respModel = new RespModel();
        Assert.isTrue((file != null), "清选择文件上传");

        String name = file.getOriginalFilename();
        Assert.isTrue(StringUtils.isNotBlank(name), "文件没有名称");

        // 文件后缀
        String filePostfix = FilenameUtils.getExtension(name);
        String fileType = "doc,ppt,pdf,pptx,docx,jpg,jpeg,png,gif,zip,xls,xlsx";
        Assert.isTrue(fileType.contains(filePostfix.toLowerCase()), "文件类型错误");

        Assert.isTrue(file.getSize() < 50 * 1024 * 1024, "文件不能大于50mb!");

        String url = yxcspUtil.push(file);
        respModel.name = name;
        respModel.url = url;
        respModel.success = true;
        respModel.setMessage("上传成功");
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 文件名称
        public String name;
        // 文件标题
        public String title;
        // 文件url
        public String url;

        public boolean success = true;
        public String msg;
    }
}
